package com.autoglm.ui.components

import androidx.compose.animation.core.*
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Mic
import androidx.compose.material.icons.filled.MicOff
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.scale
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.autoglm.ui.theme.*

/**
 * 语音按钮组件
 * 支持录音状态的动画效果
 */
@Composable
fun VoiceButton(
    modifier: Modifier = Modifier,
    isListening: Boolean = false,
    isProcessing: Boolean = false,
    onClick: () -> Unit = {}
) {
    // 脉冲动画
    val pulseAnimation by animateFloatAsState(
        targetValue = if (isListening) 1.2f else 1.0f,
        animationSpec = infiniteRepeatable(
            animation = tween(1000, easing = EaseInOut),
            repeatMode = RepeatMode.Reverse
        ),
        label = "pulse"
    )
    
    // 缩放动画
    val scaleAnimation by animateFloatAsState(
        targetValue = if (isProcessing) 0.9f else 1.0f,
        animationSpec = tween(200, easing = EaseInOut),
        label = "scale"
    )
    
    Box(
        modifier = modifier,
        contentAlignment = Alignment.Center
    ) {
        // 外层光晕
        if (isListening) {
            Box(
                modifier = Modifier
                    .size(100.dp)
                    .scale(pulseAnimation)
                    .background(
                        brush = Brush.radialGradient(
                            colors = listOf(
                                GlowEffect,
                                Color.Transparent
                            )
                        ),
                        shape = CircleShape
                    )
            )
        }
        
        // 主按钮
        Box(
            modifier = Modifier
                .size(80.dp)
                .scale(scaleAnimation)
                .clip(CircleShape)
                .background(
                    brush = when {
                        isListening -> Brush.linearGradient(
                            colors = listOf(
                                Accent500,
                                Accent400
                            )
                        )
                        isProcessing -> Brush.linearGradient(
                            colors = listOf(
                                Warning500,
                                Warning400
                            )
                        )
                        else -> Brush.linearGradient(
                            colors = listOf(
                                GradientPrimaryStart,
                                GradientPrimaryEnd
                            )
                        )
                    }
                )
                .border(
                    width = 3.dp,
                    brush = Brush.linearGradient(
                        colors = listOf(
                            Accent300,
                            Accent500
                        )
                    ),
                    shape = CircleShape
                )
                .clickable { onClick() },
            contentAlignment = Alignment.Center
        ) {
            Icon(
                imageVector = if (isListening) Icons.Default.MicOff else Icons.Default.Mic,
                contentDescription = if (isListening) "停止录音" else "开始录音",
                tint = Color.White,
                modifier = Modifier.size(32.dp)
            )
        }
        
        // 处理状态指示器
        if (isProcessing) {
            Box(
                modifier = Modifier
                    .size(24.dp)
                    .background(
                        color = Warning500,
                        shape = CircleShape
                    ),
                contentAlignment = Alignment.Center
            ) {
                CircularProgressIndicator(
                    modifier = Modifier.size(16.dp),
                    color = Color.White,
                    strokeWidth = 2.dp
                )
            }
        }
    }
}
